﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using fingerWinForm.Utilities;

namespace fingerWinForm.GUI
{
    public partial class AddUserWindow : Form
    {
        public AddUserWindow()
        {
            InitializeComponent();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if (CheckInput.isEmpty(userNameTextBox.Text) ||
                CheckInput.isEmpty(passwordTextBox.Text) ||
                CheckInput.isEmpty(roleComboBox.Text) )
            {
                MessageBox.Show("Please fill all the fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(MainWindow.startingPath + "Users.xml");
            XmlNode rootNode = xmlDoc.CreateElement("user");
            XmlNode username = xmlDoc.CreateElement("username");
            XmlNode password = xmlDoc.CreateElement("password");
            XmlNode role = xmlDoc.CreateElement("role");
            username.InnerText = userNameTextBox.Text;
            password.InnerText = passwordTextBox.Text;
            role.InnerText = roleComboBox.Text;
            rootNode.AppendChild(username);
            rootNode.AppendChild(password);
            rootNode.AppendChild(role);
            xmlDoc.DocumentElement.AppendChild(rootNode);
            xmlDoc.Save(MainWindow.startingPath + "Users.xml");  
            Close();
        }
    }
}
